package net.molchat.spring.util;


import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;


/**
 * @author Valentine Markovich (vmarkovich@gorillagroup.com), Gorilla Inc.
 * 
 */
public abstract class SpringUtil {

/**
 * @return UserDetails or null, if user not authorized.
 */
public static UserDetails getUser() {

	UserDetails userDetails = null;

	try {
		Authentication auth = SecurityContextHolder.getContext()
				.getAuthentication();
		if (auth != null) {
			if (!(auth instanceof AnonymousAuthenticationToken)) {
				userDetails = (UserDetails) auth.getPrincipal();
			}
		}
	} catch (Exception e) {
		e.printStackTrace();
	}

	return userDetails;
}


}
